<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="物料编码:" prop="materialNo">
            <el-input v-model="formModel.materialNo" :readonly="readonly" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料描述:" prop="materialDesc">
            <el-input v-model="formModel.materialDesc" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位:" prop="unit">
            <el-input v-model="formModel.unit" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料组:" prop="materialTypeKey">
            <hey-select v-model="formModel.materialTypeKey" :clearable="false" clcd="key_material_type" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="formModel.materialTypeKey==='car'" label="产品组:" prop="productTypeKey">
            <hey-select v-model="formModel.productTypeKey" :clearable="false" clcd="key_product_type" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态:" prop="status">
            <hey-select v-model="formModel.status" :clearable="false" clcd="is_effective_cd" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'MaterialRelationMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      // 只读
      readonly: false,
      formModel: {
        materialDataId: '',
        materialNo: '',
        materialDesc: '',
        unit: '',
        materialTypeKey: '',
        productTypeKey: '',
        status: '1'
      },
      // 老数据
      oldFormModel: {
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.oldFormModel = JSON.parse(JSON.stringify(this.formModel))
        this.readonly = true
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        materialNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        materialDesc: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        unit: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        materialTypeKey: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        productTypeKey: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        status: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // 如果物料组不为整车则清空产品组数据
          if (this.formModel.materialTypeKey !== 'car') {
            this.formModel.productTypeKey = ''
          }
          if (this.formModel.materialDataId !== '') {
            // 判断是否修改
            if (JSON.stringify(this.formModel) !== JSON.stringify(this.oldFormModel)) {
              const tip = '修改成功！'
              const url = '/materialData/update'
              this.post(url, this.formModel).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            } else {
              this.$emit('mntSuc')
              this.closeDialog()
            }
          } else {
            const url = '/materialData/save'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
